using UnityEngine;
using UI.BankingPanel.Data;
using System.Collections.Generic;

/// <summary>
/// Excel分组导出功能演示
/// 展示相同金额的号码如何分组显示
/// </summary>
public class ExcelGroupingDemo : MonoBehaviour
{
    [Header("演示设置")]
    [SerializeField] private bool autoRunDemo = false;
    
    private void Start()
    {
        if (autoRunDemo)
        {
            RunGroupingDemo();
        }
    }
    
    /// <summary>
    /// 运行分组演示
    /// </summary>
    [ContextMenu("运行分组演示")]
    public void RunGroupingDemo()
    {
        Debug.Log("=== Excel分组导出功能演示 ===");
        
        // 创建演示数据
        var demoData = CreateDemoData();
        
        // 导出Excel文件
        var result = SimpleExcelExporter.ExportBankingDataToExcel(demoData, "分组演示数据");
        
        if (result.Success)
        {
            Debug.Log($"✅ 分组演示导出成功！");
            Debug.Log($"📁 文件路径: {result.FilePath}");
            Debug.Log("📊 Excel文件中的数据将按以下方式分组显示：");
            Debug.Log("");
            
            // 显示分组预览
            ShowGroupingPreview(demoData);
        }
        else
        {
            Debug.LogError($"❌ 分组演示导出失败: {result.Message}");
        }
    }
    
    /// <summary>
    /// 创建演示数据
    /// </summary>
    private BankingData CreateDemoData()
    {
        var bankingData = new BankingData();
        bankingData.odds = 42.0f;
        
        // 创建具有相同金额的测试数据
        var demoNumbers = new Dictionary<int, (float total, float eat, float report)>
        {
            // 100元组：01,02,03,04
            {1, (100f, 50f, 50f)},
            {2, (100f, 50f, 50f)},
            {3, (100f, 50f, 50f)},
            {4, (100f, 50f, 50f)},
            
            // 50元组：05,06,07,08
            {5, (50f, 25f, 25f)},
            {6, (50f, 25f, 25f)},
            {7, (50f, 25f, 25f)},
            {8, (50f, 25f, 25f)},
            
            // 200元组：10,11
            {10, (200f, 100f, 100f)},
            {11, (200f, 100f, 100f)},
            
            // 75元组：15,16,17
            {15, (75f, 30f, 45f)},
            {16, (75f, 30f, 45f)},
            {17, (75f, 30f, 45f)},
            
            // 120元组：20
            {20, (120f, 60f, 60f)},
            
            // 90元组：25,26,27,28,29
            {25, (90f, 40f, 50f)},
            {26, (90f, 40f, 50f)},
            {27, (90f, 40f, 50f)},
            {28, (90f, 40f, 50f)},
            {29, (90f, 40f, 50f)},
            
            // 80元组：35,36
            {35, (80f, 35f, 45f)},
            {36, (80f, 35f, 45f)},
            
            // 110元组：40
            {40, (110f, 55f, 55f)},
            
            // 95元组：45,46,47
            {45, (95f, 45f, 50f)},
            {46, (95f, 45f, 50f)},
            {47, (95f, 45f, 50f)}
        };
        
        Debug.Log($"📋 创建了包含 {demoNumbers.Count} 个号码的演示数据");
        Debug.Log("💰 数据包含多个相同金额的分组，将在Excel中合并显示");
        
        return bankingData;
    }
    
    /// <summary>
    /// 显示分组预览
    /// </summary>
    private void ShowGroupingPreview(BankingData bankingData)
    {
        Debug.Log("📊 === 总数据分组预览 ===");
        Debug.Log("格式：[号码列表] -> 金额");
        Debug.Log("200元: [10,11] -> 200");
        Debug.Log("120元: [20] -> 120");
        Debug.Log("110元: [40] -> 110");
        Debug.Log("100元: [01,02,03,04] -> 100");
        Debug.Log("95元:  [45,46,47] -> 95");
        Debug.Log("90元:  [25,26,27,28,29] -> 90");
        Debug.Log("80元:  [35,36] -> 80");
        Debug.Log("75元:  [15,16,17] -> 75");
        Debug.Log("50元:  [05,06,07,08] -> 50");
        Debug.Log("");
        Debug.Log("📊 === 自己吃码分组预览 ===");
        Debug.Log("100元: [10,11] -> 100");
        Debug.Log("60元:  [20] -> 60");
        Debug.Log("55元:  [40] -> 55");
        Debug.Log("50元:  [01,02,03,04] -> 50");
        Debug.Log("45元:  [45,46,47] -> 45");
        Debug.Log("40元:  [25,26,27,28,29] -> 40");
        Debug.Log("35元:  [35,36] -> 35");
        Debug.Log("30元:  [15,16,17] -> 30");
        Debug.Log("25元:  [05,06,07,08] -> 25");
        Debug.Log("");
        Debug.Log("📊 === 上报数据分组预览 ===");
        Debug.Log("100元: [10,11] -> 100");
        Debug.Log("60元:  [20] -> 60");
        Debug.Log("55元:  [40] -> 55");
        Debug.Log("50元:  [01,02,03,04,25,26,27,28,29,45,46,47] -> 50");
        Debug.Log("45元:  [15,16,17,35,36] -> 45");
        Debug.Log("25元:  [05,06,07,08] -> 25");
        Debug.Log("");
        Debug.Log("✨ 在Excel文件中，相同金额的号码会自动合并在一行显示！");
        Debug.Log("📝 例如：'01,02,03,04' 会显示在一个单元格中，对应金额100");
    }
    
    /// <summary>
    /// 显示分组规则说明
    /// </summary>
    [ContextMenu("显示分组规则")]
    public void ShowGroupingRules()
    {
        Debug.Log("=== Excel分组导出规则说明 ===");
        Debug.Log("");
        Debug.Log("🔄 分组逻辑：");
        Debug.Log("1. 按金额大小分组：相同金额的号码会被分到一组");
        Debug.Log("2. 组内排序：每组内的号码按数字大小排序");
        Debug.Log("3. 组间排序：不同金额的组按金额从大到小排序");
        Debug.Log("4. 显示格式：号码用逗号分隔，如 '01,02,03'");
        Debug.Log("");
        Debug.Log("📋 Excel表格格式：");
        Debug.Log("| 号码        | 金额 |");
        Debug.Log("|-------------|------|");
        Debug.Log("| 10,11       | 200  |");
        Debug.Log("| 01,02,03,04 | 100  |");
        Debug.Log("| 45,46,47    | 95   |");
        Debug.Log("| 05,06,07,08 | 50   |");
        Debug.Log("");
        Debug.Log("✅ 优势：");
        Debug.Log("- 数据更紧凑，易于阅读");
        Debug.Log("- 相同金额一目了然");
        Debug.Log("- 减少表格行数");
        Debug.Log("- 便于快速统计和分析");
    }
} 